// model/doctor.go
package model

import (
	"fmt"
	"gorm.io/gorm"
)

type Doctor struct {
	ID                int64   `gorm:"primaryKey" json:"id"`
	Name              string  `json:"name"`
	Title             string  `json:"title"`              // 职称
	Hospital          string  `json:"hospital"`           // 所属医院名称
	HospitalID        int64   `json:"hospital_id"`        // 医院ID
	Department        string  `json:"department"`         // 科室
	Expertise         string  `json:"expertise"`          // 擅长领域
	Avatar            string  `json:"avatar"`             // 头像URL
	Introduction      string  `json:"introduction"`       // 个人简介
	Rating            float64 `json:"rating"`             // 评分（0~5）
	ConsultationPrice int32   `json:"consultation_price"` // 问诊价格（单位：分）
	IsAvailable       bool    `json:"is_available"`       // 是否可预约
	CreatedAt         string  `json:"created_at"`
	UpdatedAt         string  `json:"updated_at"`
}

//func (Doctor) TableName() string {
//	return "doctors"
//}

// ==================== 模型方法 ====================

// GetByID 根据 ID 获取医生详情
func (d *Doctor) GetByID(db *gorm.DB, id int64) error {
	return db.Where("id = ?", id).First(d).Error
}

// ListAll 分页获取医生列表（支持关键词）
func (d *Doctor) ListAll(db *gorm.DB, keyword string, page, pageSize int32) ([]Doctor, int64, error) {
	var doctors []Doctor
	var total int64

	query := db.Model(d)

	if keyword != "" {
		k := "%" + keyword + "%"
		query = query.Where("name LIKE ? OR title LIKE ? OR expertise LIKE ?", k, k, k)
	}

	query.Count(&total)

	offset := (page - 1) * pageSize
	if err := query.Offset(int(offset)).Limit(int(pageSize)).Find(&doctors).Error; err != nil {
		return nil, 0, err
	}

	return doctors, total, nil
}

// ListByHospital 按医院ID分页查询
func (d *Doctor) ListByHospital(db *gorm.DB, hospitalID int64, page, pageSize int32) ([]Doctor, int64, error) {
	var doctors []Doctor
	var total int64

	query := db.Model(d).Where("hospital_id = ?", hospitalID)
	query.Count(&total)

	offset := (page - 1) * pageSize
	if err := query.Offset(int(offset)).Limit(int(pageSize)).Find(&doctors).Error; err != nil {
		return nil, 0, err
	}

	return doctors, total, nil
}

// ListByDepartment 按科室分页查询
func (d *Doctor) ListByDepartment(db *gorm.DB, department string, page, pageSize int32) ([]Doctor, int64, error) {
	var doctors []Doctor
	var total int64

	query := db.Model(d).Where("department = ?", department)
	query.Count(&total)

	offset := (page - 1) * pageSize
	if err := query.Offset(int(offset)).Limit(int(pageSize)).Find(&doctors).Error; err != nil {
		return nil, 0, err
	}

	return doctors, total, nil
}

// ListByDisease 按疾病关键词（在 expertise 中模糊匹配）分页查询
func (d *Doctor) ListByDisease(db *gorm.DB, disease string, page, pageSize int32) ([]Doctor, int64, error) {
	if disease == "" {
		return nil, 0, fmt.Errorf("disease 不能为空")
	}

	var doctors []Doctor
	var total int64

	k := "%" + disease + "%"
	query := db.Model(d).Where("expertise LIKE ?", k)
	query.Count(&total)

	offset := (page - 1) * pageSize
	if err := query.Offset(int(offset)).Limit(int(pageSize)).Find(&doctors).Error; err != nil {
		return nil, 0, err
	}

	return doctors, total, nil
}
